package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacDescOption;
import com.ysd.lis.mapper.bac.BacDescOptionMapper;
import com.ysd.lis.request.bac.BacDescOptionDto;
import com.ysd.lis.service.bac.BacDescOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 细菌描述性结果选项 服务实现类
 * </p>
 *
 * @author whx
 * @since 2025-01-24
 */
@Service
public class BacDescOptionServiceImpl extends ServiceImpl<BacDescOptionMapper, BacDescOption> implements BacDescOptionService {
    @Autowired
    BacDescOptionMapper bacDescOptionMapper;

    @Override
    public Result findBacDescOptionList(BacDescOption bacDescOption) {
        List<BacDescOption> list = findBacDescOptionArray(bacDescOption);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacDescOption> findBacDescOptionArray(BacDescOption bacDescOption) {
        MPJLambdaWrapper<BacDescOption> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacDescOption::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacDescOption.getOptNo()), BacDescOption::getOptNo, bacDescOption.getOptNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDescOption.getOptNa()), BacDescOption::getOptNa, bacDescOption.getOptNa());
        queryWrapper.eq(StringUtils.isNotBlank(bacDescOption.getInputCode()), BacDescOption::getInputCode, bacDescOption.getInputCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacDescOption.getGermNo()), BacDescOption::getGermNo, bacDescOption.getGermNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacDescOption.getGermNa()), BacDescOption::getGermNa, bacDescOption.getGermNa());
        queryWrapper.orderByAsc(BacDescOption::getSeq);

        return bacDescOptionMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacDescOption(BacDescOption bacDescOption) {
        int i = bacDescOptionMapper.insert(bacDescOption);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacDescOption);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacDescOption(BacDescOption bacDescOption) {
        bacDescOption.setDelFlag(1);
        int i = bacDescOptionMapper.updateById(bacDescOption);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacDescOption);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacDescOption(BacDescOption bacDescOption) {
        int i = bacDescOptionMapper.updateById(bacDescOption);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacDescOption);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result updateBatchBacDescOption(BacDescOptionDto dto) {
        List<BacDescOption> bacDescOptionList = dto.getBacDescOptionList();
        boolean b = this.saveOrUpdateBatch(bacDescOptionList);
        if (b) {
            return Result.succ(1, "批量更新成功", bacDescOptionList);
        } else {
            return Result.fail("批量更新失败");
        }

    }
}
